for loop in C Program

06-11-17 Course- C

For loop even in C language, a part of the statement or program is used to repeat several times, such as time and two-while loop.

However, we can initialize and increase or decrease the variable even while examining the position for the loop.

While expression for the opposite, position or loop while loop, the expression is given before the statement, so it can execute the statement 0 times or more.

When use for loop in C

For loop is better if number of iteration is known by the programmer.

Syntax of for loop in C

The syntax of for loop in c language is given below:


for(initialization;condition;incr/decr)
{  
//code to be executed  
}  

Flowchart of for loop in C

FOR LOOP IN C

Example of for loop in C language

Let's see the simple program of for loop that prints table of 1.


#include <stdio.h>      
#include <conio.h>      
void main(){      
int i=0;    
clrscr();      
    
for(i=1;i<=10;i++){    
printf("%d \n",i);    
}   
      
getch();      
}      

Output


1
2
3
4
5
6
7
8
9
10

C Program : Print table for the given number using C for loop


 
#include <stdio.h>    
#include <conio.h>    
void main(){    
int i=1,number=0;  
clrscr();    
  
printf("Enter a number: ");  
scanf("%d",&number);  
  
for(i=1;i<=10;i++){    
printf("%d \n",(number*i));  
}  
  
getch();    
}    

Output


Enter a number: 2
2
4
6
8
10
12
14
16
18
20
Enter a number: 1000
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000

Infinitive for loop in C

If you don't initialize any variable, check condition and increment or decrement variable in for loop, it is known as infinitive for loop.

In other words, if you place 2 semicolons in for loop, it is known as infinitive for loop.


 
for(;;){  
printf("infinitive for loop example by javatpoint");  
}  

If you run this program, you will see above statement infinite times.